package com.atguigu.spzx.manager.task;


import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStaMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class OrderStaTask {

    //把前一天定时数据 保存在order_statistics里面

    @Autowired
    OrderStaMapper orderStaMapper;

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Scheduled(cron = "2 * * * * ?")
    public void orderSta(){
        //        SELECT DATE(create_time) ,SUM(total_amount) FROM `order_info`
        //        WHERE DATE(create_time) = '2024-03-01'
        //        GROUP BY DATE(create_time)

        String preDate = new DateTime().plusDays(-1).toString("yyyy-MM-dd");

        //orderStatistics是根据preDate查询到的前一天信息 包含 orderDate totalAmount
        OrderStatistics orderStatistics = orderInfoMapper.getOrderSta(preDate);

        if(orderStatistics!=null){
            //为真查询前一天的orderInfo数据库
           Integer count =  orderStaMapper.getByPreDate(preDate);

            if(count==0){
                orderStaMapper.addOrerSta(orderStatistics);
            }
        }

        System.out.println(new DateTime());
    }



}
